package com.lpwa.system.service.impl;

import java.util.List;

import com.lpwa.common.utils.StringUtils;
import com.lpwa.system.domain.*;
import com.lpwa.system.mapper.AssetMapper;
import com.lpwa.system.mapper.LableMapper;
import com.lpwa.system.mapper.TagAssetMapper;
import com.lpwa.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lpwa.common.core.text.Convert;

/**
 * 资产 服务层实现
 * 
 * @author tanmu
 * @date 2019-12-17
 */
@Service
public class AssetServiceImpl implements IAssetService
{
	@Autowired
	private AssetMapper assetMapper;
	@Autowired
	private LableMapper lableMapper;
	@Autowired
	private IDeviceModelService deviceModelService;
	@Autowired
	private ISysConfigService configService;
	@Autowired
	private ISysDeptService deptService;
	@Autowired
	private ILableService lableService;
	@Autowired
	private TagAssetMapper tagAssetMapper;

	/**
     * 查询资产信息
     * 
     * @param id 资产ID
     * @return 资产信息
     */
    @Override
	public Asset selectAssetById(Long id)
	{
	    return assetMapper.selectAssetById(id);
	}
	
	/**
     * 查询资产列表
     * 
     * @param asset 资产信息
     * @return 资产集合
     */
	@Override
	public List<Asset> selectAssetList(Asset asset)
	{
	    return assetMapper.selectAssetList(asset);
	}
	
    /**
     * 新增资产
     * 
     * @param asset 资产信息
     * @return 结果
     */
	@Override
	public int insertAsset(Asset asset)
	{
		String user = asset.getCreateBy();
		asset = bindLableId(asset,user);
//		return personMapper.insertPerson(person);
	    return assetMapper.insertAsset(asset);
	}
	
	/**
     * 修改资产
     * 
     * @param asset 资产信息
     * @return 结果
     */
	@Override
	public int updateAsset(Asset asset)
	{
	    return assetMapper.updateAsset(asset);
	}

	/**
     * 删除资产对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteAssetByIds(String ids)
	{
		return assetMapper.deleteAssetByIds(Convert.toStrArray(ids));
	}
	/**
	 * 人员和标签之间的绑定逻辑
	 *
	 * @String lableId 标签ID
	 * @String personId 人员ID
	 * @String greateBy 用户
	 * @return 结果true 绑定成功 FALSE绑定失败
	 */
	public Asset bindLableId(Asset asset, String user) {
		String lableId = asset.getTagId();
		String assetId = asset.getAssetId();
		if (!StringUtils.isEmpty(lableId)){
			//判断标签Id在标签表中是否存在
			//查询标签表
			Tag lable = lableMapper.selectLableById(lableId);
			if (StringUtils.isNull(lable)){//为空情况
				//新增标签
				Tag newlable = new Tag();
				newlable.setTagId(lableId);
				//根据标签Id判断标签型号
				String modelCode = lableId.substring(8,10);
				DeviceModel model = deviceModelService.selectDeviceModelByCode(modelCode);
				if (StringUtils.isNull(model)){
					throw new RuntimeException("标签Id没有匹配对应型号");
				}else{
					newlable.setTagType(model.getModelId().toString());//标签类型
				}
				newlable.setCreateBy(user);
				newlable.setIsBind(1);//将标签的绑定状态改为“已绑定”
				newlable.setAssetId(assetId);//设置标签的绑定人员ID
				int bool = lableService.insertLable(newlable);
				//保存标签和人员关系
//				TagPerson lablePerson = new TagPerson();
//				lablePerson.setTagId(lableId);
//				lablePerson.setPersonId(personId);
//				lablePerson.setIsBind("1");
//				lablePerson.setCreateBy(user);
//				lablePersonMapper.insertLablePerson(lablePerson);
//				person.setIsBind(1);
				//保存标签和资产的关系
				TagAsset tagAsset = new TagAsset();
				tagAsset.setTagId(lableId);
				tagAsset.setAssetId(assetId);
				tagAsset.setIsBind("1");
				tagAsset.setCreateBy(user);
				tagAssetMapper.insertTagAsset(tagAsset);
				asset.setIsBind(1);
			}else {//非空情况，标签已绑定，标签未绑定
				//判断标签是否绑定
				Integer isBind = lable.getIsBind();
				if (isBind==1){//已绑定
					throw new RuntimeException("该标签已绑定，请先解绑");
				}else {
					//保存标签和资产关系
					TagAsset tagAsset = new TagAsset();
					tagAsset.setTagId(lableId);
					tagAsset.setAssetId(assetId);
					tagAsset.setIsBind("1");
					tagAsset.setCreateBy(user);
					tagAssetMapper.insertTagAsset(tagAsset);
					//标签表的绑定状态也改为已绑定
					Tag lab = new Tag();
					lab.setTagId(lableId);
					lab.setIsBind(1);
					//设置标签的绑定人员ID
					lab.setAssetId(assetId);
					lableMapper.updateLable(lab);
					asset.setIsBind(1);
				}
			}
		}else{//标签Id为空，说明未绑定，修改状态下说明该人员解绑标签
			asset.setIsBind(0);
		}
		return asset;
	}
}
